package com.itheima.wms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.base.BaseController;
import com.itheima.pinda.base.R;
import com.itheima.pinda.base.entity.SuperEntity;
import com.itheima.wms.dto.OwnerDTO;
import com.itheima.wms.entity.OwnerEntity;
import com.itheima.wms.service.OwnerService;
import com.itheima.wms.vo.OwnerDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/owner")
public class OwnerController extends BaseController {

    @Autowired
    private OwnerService ownerService;

    /**
     * 分页查询货主管理
     * @param data
     * @return
     */
    @GetMapping("/page")
    public R<IPage<OwnerEntity>> page(@RequestParam Map data){
        Page<OwnerEntity> page = getPage();
        IPage<OwnerEntity> ownerPage = ownerService.page(page,data);
        page.setRecords(ownerPage.getRecords());
        return R.success(page);
    }

    /**
     * 查询所有货主
     * @param data
     * @return
     */
    @GetMapping("/list")
    public R<List<OwnerEntity>> list(@RequestParam Map data){
        List<OwnerEntity> ownerArray = ownerService.list(data);
        return R.success(ownerArray);
    }

    @GetMapping("/vague")
    public R<List<OwnerEntity>> list(String params){
        LambdaQueryWrapper<OwnerEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(OwnerEntity::getCode, params).or().like(OwnerEntity::getName, params);
        List<OwnerEntity> ownerArray = ownerService.list(wrapper);
        return success(ownerArray);
    }

    @GetMapping("/{id}")
    public R<OwnerEntity> get(@PathVariable Long id){
        return success(ownerService.getById(id));
    }

    @GetMapping("detail/{id}")
    public R<OwnerDetailVO> getDetail(@PathVariable Long id){
        return success(ownerService.getDetail(id));
    }

    /**
     * 新增货主管理
     * @param data
     * @return
     */
    @PostMapping
    public R<OwnerEntity> save(@RequestBody OwnerDTO data) {
        ownerService.save(data);
        return success(data);
    }

    /**
     * 修改货主管理
     * @param data
     * @return
     */
    @PutMapping
    public R<OwnerEntity> update(@RequestBody  OwnerDTO data) {
        ownerService.updateById(data);
        return success(data);
    }

    /**
     * 删除货主管理
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("ids[]") List<Long> ids) {
        ownerService.removeByIds(ids);
        return success();
    }
}
